package lion.blog.util;

import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import lion.dev.io.FileUtil;
import lion.dev.lang.MapJ;
import lion.framework.core.util.AppPaths;
import lion.framework.core.web.ApplicationContext;
import lion.framework.core.web.path.ParameterizedPath;
import lion.framework.core.web.path.PathVar;

/**
 * @author lion
 * @email hl_0074@sina.com
 * @date 2015年9月23日
 */
public class BlogUtil {

	public static final String BLOG_ARTICLE_DEFAULT_URL_PATTERN = "{id}";
	private static final Map<String, Boolean> pathExistsMap = new HashMap<>();

	public static String getBlogLink(MapJ article, String catelog) {

		if (article == null) { return ""; }
		String namespace = PathVar.getVar("blog");
		String context = (String) ApplicationContext.get("webcontext");

		MapJ setting = (MapJ) ApplicationContext.get("blog");
		String pattern = "/post/{id}";
		if (setting != null) {
			pattern = setting.getString("BlogURLType", BLOG_ARTICLE_DEFAULT_URL_PATTERN);
		}
		if (StringUtils.isBlank(pattern)) {
			pattern = BLOG_ARTICLE_DEFAULT_URL_PATTERN;
		}

		Date date = article.getDate("publishTime");

		Calendar c = Calendar.getInstance();
		c.setTime(date);

		article.put("yyyy", c.get(Calendar.YEAR));
		article.put("MM", StringUtils.leftPad("" + c.get(Calendar.MONTH), 2, '0'));
		article.put("dd", StringUtils.leftPad("" + c.get(Calendar.DATE), 2, '0'));

		article.put("yyyyMMdd", article.getString("yyyy") + "" + article.getString("MM") + "" + article.getString("dd"));

		article.put("catelog", catelog);

		String cate = "";
		if (StringUtils.isNotBlank(catelog)) {
			cate = catelog + "/";
		}
		String pathPattern = context + "/" + namespace + "/post/" + cate + pattern;

		return ParameterizedPath.mapPath(FileUtil.toUnixPath(pathPattern), article);
	}

	public static boolean hasFile(String path) {

		MapJ setting = (MapJ) ApplicationContext.get("blog");

		String theme = "default";
		if (setting != null) {
			theme = setting.getString("theme");
		}
		if (StringUtils.isBlank(theme)) {
			theme = "default";
		}

		path = AppPaths.WEBINF + FileUtil.toUnixPath("/blog/theme/" + theme + "/" + path);

		Boolean result = pathExistsMap.get(path);

		if (result == null) {
			File file = new File(path);
			result = file.exists();
			pathExistsMap.put(path, result);
		}
		return result;
	}

	public static String getPath(String file) {

		MapJ setting = (MapJ) ApplicationContext.get("blog");

		String theme = "default";
		if (setting != null) {
			theme = setting.getString("theme");
		}
		if (StringUtils.isBlank(theme)) {
			theme = "default";
		}

		return FileUtil.toUnixPath("/WEB-INF/blog/theme/" + theme + "/" + file);
	}

	public static String getAbsolutePath(String file) {

		MapJ setting = (MapJ) ApplicationContext.get("blog");

		String theme = "default";
		if (setting != null) {
			theme = setting.getString("theme");
		}
		if (StringUtils.isBlank(theme)) {
			theme = "default";
		}

		return AppPaths.WEBINF + FileUtil.toUnixPath("/blog/theme/" + theme + "/" + file);
	}

	public static String getCurrentTheme() {

		MapJ setting = (MapJ) ApplicationContext.get("blog");
		return setting.getString("theme", "default");
	}

	public static String getSetting(String key) {

		MapJ setting = (MapJ) ApplicationContext.get("blog");
		return setting.getString(key);
	}

}
